<template>
    <WorkOrderList
      :row="row" 
      :loading="loading"
      title="设备维护维修信息 · 工单清单"
      height="66vh"
      @view="onView"
      @edit="onEdit"
      @remove="onRemove"  
      :search-api="listOrder"
    />

    <!-- <WorkOrderList :search-api="load" :size="'small'" /> -->
  </template>
  
  <script setup>
  import { ref, watch, onMounted } from 'vue'
  import WorkOrderList from './WorkOrderList.vue'
  import { getOrderInfoById,listOrder } from '@/api/sbtz/order'
  
  const props = defineProps({
    row: { type: Object, default: () => ({}) }   // 父级会传 { id, ... }
  })
  
  const loading = ref(false)
  const woRows  = ref([])    // 直接存放“数组”，避免外层再套 {rows: []}
    
 


  function adaptToRows(res) {
    // 统一把各种常见形态转成“数组”
    if (Array.isArray(res)) return res
    if (Array.isArray(res?.rows)) return res.rows
    if (Array.isArray(res?.data)) return res.data
    if (Array.isArray(res?.list)) return res.list
    // 单条也兼容为数组
    if (res && typeof res === 'object' && Object.keys(res).length) return [res]
    return []
  }
  
  async function load() {
    if (!props.row?.id) { woRows.value = []; return }
    loading.value = true
    try {
      const res = await getOrderInfoById(props.row.id) 
      woRows.value = adaptToRows(res)
      console.log(woRows.value);
    } catch (err) {
      console.error('getOrderInfoById error:', err)
      woRows.value = []
    } finally {
      loading.value = false
    }
  }
  
  // onMounted(load)
  // watch(() => props.row?.id, () => load())    
  
  function onView(row)  { /* 打开详情弹窗 */ }
  function onEdit(row)  { /* 打开编辑 */ }
  function onRemove(row){ /* 删除后刷新 */ }
  </script>
  